package com.controller;

import com.WxApiUtils;
import com.bean.User;
import com.form.UserForm;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
public class WeiXinApiController extends AbstractController {

    private String key = "Meshel0944552Meshel0944552Meshel";

    @RequestMapping("/test-{table}")
    @ResponseBody
    public Object test(@PathVariable("table") String table, String name) {
        System.out.println(table + ", " + name);

        UserForm userForm = new UserForm();
        User user = new User();
        user.setId(1);
        user.setName("ADL");
        user.setDate(new Date());

        User admin = new User();
        admin.setId(2);
        admin.setName("Admin");

        userForm.user = user;
        userForm.admin = admin;
        return userForm;
    }

    @RequestMapping("/json")
    @ResponseBody
    public Object test(@RequestBody UserForm userForm) {
        System.out.println(userForm.user);
        System.out.println(userForm.admin);
        return "接受JSON";
    }

    @RequestMapping(value = "/qrcode", produces = "image/png")
    @ResponseBody
    public Object test(String content, HttpServletResponse response) throws Exception {

        int width = 250; // 图像宽度
        int height = 250; // 图像高度
        String format = "png";// 图像类型
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
                BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
        MatrixToImageWriter.writeToStream(bitMatrix, format, response.getOutputStream());// 输出图像

        return null;
    }

    @RequestMapping(value = "/xiadan", produces = "text/html;charset=utf-8")
    @ResponseBody
    public Object 下单(HttpServletResponse response) throws Exception {
        String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        WxApiUtils wxApiUtils = new WxApiUtils(servletContext.getRealPath("WEB-INF/cert/apiclient_cert.p12"), "1374428802");

        SortedMap<String, String> paramMap = new TreeMap<>();
        paramMap.put("appid", "wx216c6100247f014b");//公众账号ID
        paramMap.put("mch_id", "1374428802");//商户号
        paramMap.put("nonce_str", "5K8264ILTKCH16CQ2502SI8ZNMTM67VS");//随机字符串
        paramMap.put("body", "Mesh-el微信支付测试");//商品描述
        paramMap.put("out_trade_no", "DDH-" + System.currentTimeMillis());//商户订单号
        paramMap.put("total_fee", "1");//总金额
        paramMap.put("spbill_create_ip", "124.117.120.36");//终端IP
        paramMap.put("notify_url", "http://uyghurjava.com/wxpay/pub/pay/getpackage");//通知地址

//        paramMap.put("notify_url", "http://n15n837083.imwork.net/debug/wechat_return.jhtml");//通知地址
        paramMap.put("trade_type", "NATIVE");//交易类型
        paramMap.put("product_id", "ABC-100-200-300");//商品ID
        String sign = wxApiUtils.createSign(paramMap);
        paramMap.put("sign", sign);//签名

        String map2Xml = wxApiUtils.map2Xml(paramMap);
        String post_response = wxApiUtils.post(url, map2Xml);
        Map<String, String> resultMap = wxApiUtils.xml2Map(post_response);
        System.out.println(map2Xml);
        System.out.println("-----------------");
        System.out.println(post_response);
        String code_url = resultMap.get("code_url");
        return StringEscapeUtils.escapeXml11(post_response) + "<br/><img src='qrcode?content=" + code_url + "'/>";
    }

    @RequestMapping(value = "/zhuanzhang", produces = "text/html;charset=utf-8")
    @ResponseBody
    public Object 付款到用户(HttpServletResponse response) throws Exception {
        String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
        WxApiUtils wxApiUtils = new WxApiUtils(servletContext.getRealPath("WEB-INF/cert/apiclient_cert.p12"), "1374428802");

        SortedMap<String, String> paramMap = new TreeMap<>();
        paramMap.put("mch_appid", "wx216c6100247f014b");//公众账号ID
        paramMap.put("mchid", "1374428802");//商户号
        paramMap.put("nonce_str", "5K8264ILTKCH16CQ2502SI8ZNMTM67VS");//随机字符串
        paramMap.put("partner_trade_no", "DDH-" + System.currentTimeMillis());//商户订单号
        paramMap.put("openid", "o047ivskMpjmg5shAiI-pu7XOysE"/*ADL*/);//商户订单号
        paramMap.put("check_name", "NO_CHECK");//商户订单号
        paramMap.put("amount", "100");//总金额
        paramMap.put("spbill_create_ip", "124.117.122.98");//终端IP
        paramMap.put("desc", "SDK测试付款到用户");//终端IP
        String sign = wxApiUtils.createSign(paramMap);
        paramMap.put("sign", sign);//签名

        String map2Xml = wxApiUtils.map2Xml(paramMap);
        String post_response = wxApiUtils.post(url, map2Xml);
        Map<String, String> resultMap = wxApiUtils.xml2Map(post_response);
        System.out.println(map2Xml);
        System.out.println("-----------------");
        System.out.println(post_response);
        return StringEscapeUtils.escapeXml11(post_response);
    }

    @RequestMapping("/a1")
//    @ResponseBody
    public Object a1() {
        return "a1";
    }
}
